﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 国际化(I18N)
    /// </summary>
    public static class I18N
    {
        #region 属性
        /// <summary>
        /// 获取 国际化(I18N)处理程序
        /// </summary>
        public static II18nHandler I18nHandler { get; private set; }

        #endregion

        #region 方法
        /// <summary>
        /// 设置处理程序
        /// </summary>
        /// <param name="handler">国际化(I18N)处理程序</param>
        public static void SetHandler(II18nHandler handler)
        {
            I18nHandler = handler;
        }

        /// <summary>
        /// 获取国际化(I18N)字符，不存在创建
        /// </summary>
        /// <param name="locale">区域语系</param>
        /// <param name="format">复合格式字符串</param>
        /// <param name="args">用于填充占位符的参数</param>
        /// <returns>国际化(I18N)字符</returns>
        public static string T(Locale locale, string format, params object[] args)
        {
            var value = Get(locale, true, format, args);
            return value;
        }

        /// <summary>
        /// 获取国际化(I18N)字符，不存在创建
        /// </summary>
        /// <param name="format">复合格式字符串</param>
        /// <param name="args">用于填充占位符的参数</param>
        /// <returns>国际化(I18N)字符</returns>
        public static string T(string format, params object[] args)
        {
            var value = Get(null, true, format, args);
            return value;
        }

        /// <summary>
        /// 获取国际化(I18N)字符
        /// </summary>
        /// <param name="locale">区域语系</param>
        /// <param name="createIfNotexist">如果不存在，则创建</param>
        /// <param name="format">复合格式字符串</param>
        /// <param name="args">用于填充占位符的参数</param>
        /// <returns>国际化(I18N)字符</returns>
        private static string Get(Locale locale, bool createIfNotexist, string format, params object[] args)
        {
            var key = GetKey(format);
            string value = args?.Length > 0 ? string.Format(format, args) : format;
            if (key.IsMissing())
            {
                return value;
            }
            if (I18nHandler == null)
            {
                return value;
            }

            var i18nFormat = I18nHandler.Get(key, locale);
            if (i18nFormat.IsMissing() && createIfNotexist)
            {
                I18nHandler.Add(key, format, locale);
            }
            if (!i18nFormat.IsMissing())
            {
                value = args?.Length > 0 ? string.Format(i18nFormat, args) : i18nFormat;
            }

            return value;
        }
    
        /// <summary>
        /// 获取复合格式字符串的键值
        /// </summary>
        /// <param name="format">复合格式字符串</param>
        /// <returns>复合格式字符串的键值</returns>
        private static string GetKey(string format)
        {
            var key = string.Empty;
            if (format.IsMissing())
            {
                return key;
            }
            key = format.ToMD5().ToUpper();
            return key;
        }

        #endregion

    }

}
